package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcCheckRecords;
import com.ruoyi.gcgl.domain.GcProjectSgazht;
import com.ruoyi.gcgl.service.IGcCheckRecordsService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtService;

/**
 * 安全生产检查记录Controller
 * 
 * @author ruoyi
 * @date 2023-05-02
 */
@Controller
@RequestMapping("/gcgl/checkRecords")
public class GcCheckRecordsController extends BaseController
{
    private String prefix = "gcgl/checkRecords";

    @Autowired
    private IGcCheckRecordsService gcCheckRecordsService;
    @Autowired
    private IGcProjectSgazhtService gcProjectSgazhtService;
    @Autowired
    private IGcProjectService gcProjectService;

    @RequiresPermissions("gcgl:checkRecords:view")
    @GetMapping()
    public String records()
    {
        return prefix + "/records";
    }

    /**
     * 查询安全生产检查记录列表
     */
    @RequiresPermissions("gcgl:checkRecords:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcCheckRecords gcCheckRecords)
    {
        startPage();
        gcCheckRecords.setDeleteFlag(1);
        gcCheckRecords.setCreateUser(getLoginName());
        List<GcCheckRecords> list = gcCheckRecordsService.selectGcCheckRecordsList(gcCheckRecords);
        return getDataTable(list);
    }

    /**
     * 导出安全生产检查记录列表
     */
    @RequiresPermissions("gcgl:checkRecords:export")
    @Log(title = "安全生产检查记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcCheckRecords gcCheckRecords)
    {
        List<GcCheckRecords> list = gcCheckRecordsService.selectGcCheckRecordsList(gcCheckRecords);
        ExcelUtil<GcCheckRecords> util = new ExcelUtil<GcCheckRecords>(GcCheckRecords.class);
        return util.exportExcel(list, "安全生产检查记录数据");
    }

    /**
     * 新增安全生产检查记录
     */
    @GetMapping("/add/{sgazhtId}")
    public String add(ModelMap mmap,@PathVariable("sgazhtId") Long sgazhtId){
    	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(sgazhtId);
    	mmap.put("gcProjectSgazht", gcProjectSgazht);
        return prefix + "/add";
    }

    /**
     * 新增保存安全生产检查记录
     */
    @RequiresPermissions("gcgl:checkRecords:add")
    @Log(title = "安全生产检查记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcCheckRecords gcCheckRecords){
    	gcCheckRecords.setStatus(0);
    	gcCheckRecords.setDeleteFlag(1);
    	gcCheckRecords.setCreateAt(new Date());
    	gcCheckRecords.setUpdateAt(new Date());
    	gcCheckRecords.setCreateUser(getLoginName());
    	gcCheckRecords.setUpdateUser(getLoginName());
        return toAjax(gcCheckRecordsService.insertGcCheckRecords(gcCheckRecords));
    }

    /**
     * 修改安全生产检查记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcCheckRecords gcCheckRecords = gcCheckRecordsService.selectGcCheckRecordsById(id);
        mmap.put("gcCheckRecords", gcCheckRecords);
        return prefix + "/edit";
    }

    /**
     * 修改保存安全生产检查记录
     */
    @Log(title = "安全生产检查记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcCheckRecords gcCheckRecords){
    	gcCheckRecords.setUpdateAt(new Date());
    	gcCheckRecords.setUpdateUser(getLoginName());
        return toAjax(gcCheckRecordsService.updateGcCheckRecords(gcCheckRecords));
    }

    /**
     * 删除安全生产检查记录
     */
    @RequiresPermissions("gcgl:checkRecords:remove")
    @Log(title = "安全生产检查记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcCheckRecordsService.deleteGcCheckRecordsByIds(ids));
    }

    //项目负责人审核
    @RequiresPermissions("gcgl:checkRecords:psview")
    @GetMapping("/ps")
    public String showps(){
        return prefix + "/recordsxmps";
    }
    
    @PostMapping("/pslist")
    @ResponseBody
    public TableDataInfo listps(GcCheckRecords gcCheckRecords)
    {
        startPage();
        gcCheckRecords.setStatus(0);
        gcCheckRecords.setDeleteFlag(1);
        List<GcCheckRecords> list = gcCheckRecordsService.selectGcCheckRecordsList(gcCheckRecords);
        for(GcCheckRecords obj:list){
        	if(obj.getZlwj()!=null && !obj.getZlwj().equals("")){
        		obj.setZlwj(RuoYiConfig.getOnPath()+obj.getZlwj());
        	}
        }
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/yes")
    @ResponseBody
    public AjaxResult yes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcCheckRecords checkRecords = gcCheckRecordsService.selectGcCheckRecordsById(Long.parseLong(id[i].toString()));
    		if(checkRecords!=null){
    			GcCheckRecords gcCheckRecords = new GcCheckRecords();
    			gcCheckRecords.setId(Long.parseLong(id[i]));
    			gcCheckRecords.setStatus(1);
    			gcCheckRecords.setXmfzr(getLoginName());
    			gcCheckRecords.setXmfzrAt(new Date());
            	gcCheckRecordsService.updateGcCheckRecords(gcCheckRecords);
    		}
    		
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/no")
    @ResponseBody
    public AjaxResult no(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcCheckRecords checkRecords = gcCheckRecordsService.selectGcCheckRecordsById(Long.parseLong(id[i].toString()));
    		if(checkRecords!=null){
    			GcCheckRecords gcCheckRecords = new GcCheckRecords();
    			gcCheckRecords.setId(Long.parseLong(id[i]));
        		gcCheckRecords.setStatus(2);
        		gcCheckRecords.setXmfzr(getLoginName());
    			gcCheckRecords.setXmfzrAt(new Date());
        		gcCheckRecordsService.updateGcCheckRecords(gcCheckRecords);
            	
//            	GcProjectFlow gcProjectFlow = new GcProjectFlow();
//            	gcProjectFlow.setGcProjectid(other.getGcProjectid());
//            	gcProjectFlow.setGcqthtps(0);
//            	gcProjectFlow.setUpdateAt(new Date());
//            	gcProjectFlow.setUpdateUser(getLoginName());
//            	gcProjectFlowService.updateGcProjectFlow(gcProjectFlow);
    		}
    		
    	}
    	return toAjax(1);
    }
    
    /**
     * 工程其他管理总经理审批
     * @return
     */
    @RequiresPermissions("gcgl:checkRecords:anpsview")
    @GetMapping("/anps")
    public String anps(){
        return prefix + "/recordsaqps";
    }
    
    /**
     * 查询工程其他合同列表
     */
    @PostMapping("/anpslist")
    @ResponseBody
    public TableDataInfo anpslist(GcCheckRecords gcCheckRecords)
    {
        startPage();
        gcCheckRecords.setStatus(1);
        gcCheckRecords.setDeleteFlag(1);
        List<GcCheckRecords> list = gcCheckRecordsService.selectGcCheckRecordsList(gcCheckRecords);
        for(GcCheckRecords obj:list){
        	if(obj.getZlwj()!=null && !obj.getZlwj().equals("")){
        		obj.setZlwj(RuoYiConfig.getOnPath()+obj.getZlwj());
        	}
        }
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/anyes")
    @ResponseBody
    public AjaxResult anyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcCheckRecords obj = gcCheckRecordsService.selectGcCheckRecordsById(Long.parseLong(id[i].toString()));
    		if(obj!=null){
    			GcCheckRecords gcCheckRecords = new GcCheckRecords();
    			gcCheckRecords.setId(Long.parseLong(id[i]));
    			gcCheckRecords.setStatus(3);
    			gcCheckRecords.setSecure(getLoginName());
    			gcCheckRecords.setSecureAt(new Date());
    			gcCheckRecordsService.updateGcCheckRecords(gcCheckRecords);
            	
//            	GcProjectFlow gcProjectFlow = new GcProjectFlow();
//            	gcProjectFlow.setGcProjectid(other.getGcProjectid());
//            	gcProjectFlow.setGcqthtfp(1);
//            	gcProjectFlow.setGcqthtps(1);
//            	gcProjectFlow.setGcqthtfk(0);
//            	gcProjectFlow.setUpdateAt(new Date());
//            	gcProjectFlow.setUpdateUser(getLoginName());
//            	gcProjectFlowService.updateGcProjectFlow(gcProjectFlow);
    		}
    		
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/anno")
    @ResponseBody
    public AjaxResult anno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcCheckRecords obj = gcCheckRecordsService.selectGcCheckRecordsById(Long.parseLong(id[i].toString()));
    		if(obj!=null){
    			GcCheckRecords gcCheckRecords = new GcCheckRecords();
    			gcCheckRecords.setId(Long.parseLong(id[i]));
    			gcCheckRecords.setStatus(4);
    			gcCheckRecords.setSecure(getLoginName());
    			gcCheckRecords.setSecureAt(new Date());
            	gcCheckRecordsService.updateGcCheckRecords(gcCheckRecords);            	
            	
//            	GcProjectFlow gcProjectFlow = new GcProjectFlow();
//            	gcProjectFlow.setGcProjectid(other.getGcProjectid());
//            	gcProjectFlow.setGcqthtps(0);
//            	gcProjectFlow.setUpdateAt(new Date());
//            	gcProjectFlow.setUpdateUser(getLoginName());
//            	gcProjectFlowService.updateGcProjectFlow(gcProjectFlow);
    		}
    		
    	}
    	return toAjax(1);
    }
}